package com.demo.api.controller.api.role;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.api.controller.BaseController;
import com.demo.api.service.role.IRoleService;
import com.demo.api.service.user.IUserService;
import com.demo.api.vo.dept.DeptVo;
import com.demo.api.vo.user.MenuVo;
import com.demo.api.vo.user.RoleVo;
import com.demo.core.annotation.AuthToken;
import com.demo.core.response.ApiResponse;
import com.demo.core.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Tan-Bowen
 * @version 1.0
 * @description TODO
 * @date 2021/7/27 14:52
 */

@Api(value = "角色管理", tags = "角色管理")
@Slf4j
@RestController
@RequestMapping("/api/role")
public class RoleController extends BaseController {

    @Resource
    private IRoleService roleService;

    @Resource
    private IUserService userService;


    @AuthToken(checkPower = false)
    @PostMapping("/page")
    @ApiOperation(value = "角色列表查询")
    public ApiResponse<IPage<RoleVo.PageList>> rolePage(PageUtil.Pages page, RoleVo.Select select) {
        return ApiResponse.success(roleService.rolePage(getPage(page), select));
    }


    @AuthToken(checkPower = false)
    @PostMapping("/create")
    @ApiOperation(value = "角色新增")
    public ApiResponse<Void> create(@Validated RoleVo.Create request,
                                    @RequestHeader(value = "userId", required = false) Long userId) {
        request.setCreateUser(userId);
        roleService.create(request);
        return ApiResponse.success();
    }

    @AuthToken(checkPower = false)
    @PostMapping("/update")
    @ApiOperation(value = "角色编辑")
    public ApiResponse<Void> update(@Validated RoleVo.Update update,
                                    @RequestHeader(value = "userId", required = false) Long userId) {
        roleService.update(update);
        return ApiResponse.success();
    }


    @AuthToken(checkPower = false)
    @GetMapping("/getDeptRole")
    @ApiOperation(value = "获取当前部门和所有子孙级部门的角色")// 超级管理员获取的就是所有的部门角色
    public ApiResponse<List<RoleVo.PageList>> getDeptRole(){
        return ApiResponse.success(roleService.getDeptRole());
    }


    @AuthToken(checkPower = false)
    @PostMapping("/settingRolePower")
    @ApiOperation(value = "设置角色权限")
    public ApiResponse<Void> settingRolePower(@RequestBody RoleVo.SettingRolePower body){
        roleService.settingRolePower(body.getRoleId(),body.getIds());
        return ApiResponse.success();
    }

    @AuthToken(checkPower = false)
    @GetMapping("/getMenuAndAuthId")
    @ApiOperation(value = "根据角色获取菜单和权限id")
    public ApiResponse<MenuVo.MenuAuthId> getMenuAndAuthId(@RequestParam("roleId")Long roleId){
        return ApiResponse.success(userService.getMenuAndAuthId(roleId));
    }


    @AuthToken(checkPower = false)
    @PostMapping("/deleteRole")
    @ApiOperation(value = "删除角色")
    public ApiResponse<Void> deleteRole(@RequestParam("roleId")Long roleId){
        roleService.deleteRole(roleId);
        return ApiResponse.success();
    }

}